package megrez.mail.sender.domain;

import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import megrez.mail.sender.common.Snowflake;

/**
 * 模板内容
 *  
 * @author Lucky Yang
 * @since 2.6.9
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@Table(name = "mail_template_content")
public class TemplateContent {
    @Id
    @EqualsAndHashCode.Include
    @Column(updatable = false, nullable = false, unique = true)
    private String id;

    @Version
    @Column(nullable = false)
    private Integer version;

    /** 主题 */
    private String subject;
    /** 地区 */
    private Locale locale;
    /** 数据 */
    @Lob
    private String data;

    @ToString.Exclude
    @ManyToOne
    @JoinColumn(name = "templateConfigId")
    private TemplateConfig templateConfig;

    public TemplateContent() {
    }

    public TemplateContent(String subject, Locale locale, String data, TemplateConfig templateConfig) {
        this.subject = subject;
        this.locale = locale;
        this.data = data;
        this.templateConfig = templateConfig;
    }

    @PrePersist
    @PreUpdate
    public void preAction() {
        if (getId() == null) {
            setId(Snowflake.nextIdStr());
        }
    }   
}
